package com.turnengine.client.global.user.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.user.bean.IUser;
import com.turnengine.client.global.user.bean.UserSerializer;
import java.io.IOException;

/**
 * The Get User Data Serializer.
 */
public class GetUserDataSerializer extends AbstractCommandDataSerializer<IUser> implements IGetUserDataSerializer {

	/**
	 * Creates a new Get User Data Serializer.
	 */
	public GetUserDataSerializer() {
		super(GetUser.COMMAND_ID);
	}

	@Override
	public IUser readReturnValue(IDataReader reader) throws IOException {
		IUser value = new UserSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IUser value) throws IOException {
		new UserSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IUser> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();

		// Create object
		GetUser object = new GetUser();
		object.setLoginId(loginId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IUser> argument) throws IOException {
		GetUser object = (GetUser) argument;

		// Get fields
		long loginId = object.getLoginId();

		// Write fields
		writer.writeLong(loginId);
	}

	@Override
	public IExecutableCommandResponse<IUser> newResponse() {
		return new GetUserResponse();
	}
}
